﻿using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.UI.WebControls;
using ExtendedMvc.Tests.Model;
using ExtendedMvc.Tests.Model.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ExtendedMvc.Tests {


	/// <summary>
	///This is a test class for ImageLinkTest and is intended
	///to contain all ImageLinkTest Unit Tests
	///</summary>
	[TestClass()]
	public class HtmlImageLinkTest {

		private static DummyDataSource database;
		private static ModelViewPage<Dummy> view;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext { get; set; }

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		[ClassInitialize()]
		public static void MyClassInitialize(TestContext testContext) {
			database = new DummyDataSource();
			var dummy = database.Dummies.All().First();
			view = MvcMockHelpers.FakeViewPage("Index", dummy);
			if (!RouteTable.Routes.Any()) {
				RouteTable.Routes.MapRoute(
					"Default", // Route name
					"{controller}/{action}/{id}", // URL with parameters
					new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
				);
			}
		}

		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup() {
		//}
		//
		//Use TestInitialize to run code before running each test
		[TestInitialize()]
		public void MyTestInitialize() {
			view.ViewContext.ViewData.ModelState.Clear();
		}

		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup() {
		//}
		//
		#endregion

		/// <summary>
		///A test for ImageLink Constructor
		///</summary>
		[TestMethod()]
		public void HtmlImageLink_WithUrl() {
			// Arrange
			var linkImageSource = "~/images/dummy.jpg";
			var imageSourceOutput = "/images/dummy.jpg";
			var imageLinkUrl = "~/imageLink/dummy.aspx";
			var outputUrl = "/imageLink/dummy.aspx";
			var expectedOutput = string.Format(@"<a href=""{1}""><image src=""{0}"" /></a>", imageSourceOutput, outputUrl);

			// Act
			var imageLink = view.Html.ImageLink(linkImageSource, imageLinkUrl);
			var output = imageLink.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for ImageLink Constructor
		///</summary>
		[TestMethod()]
		public void HtmlActionImageLink_WithUrl() {
			// Arrange
			var linkImageSource = "~/images/dummy.jpg";
			var imageSourceOutput = "/images/dummy.jpg";
			var outputUrl = "/";
			var expectedOutput = string.Format(@"<a href=""{1}""><image src=""{0}"" /></a>", imageSourceOutput, outputUrl);

			// Act
			var imageLink = view.Html.ActionImageLink(linkImageSource);
			var output = imageLink.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for ImageLink Constructor
		///</summary>
		[TestMethod()]
		public void HtmlRouteImageLink_WithUrl() {
			// Arrange
			var linkImageSource = "~/images/dummy.jpg";
			var imageSourceOutput = "/images/dummy.jpg";
			var outputUrl = "/";
			var expectedOutput = string.Format(@"<a href=""{1}""><image src=""{0}"" /></a>", imageSourceOutput, outputUrl);

			// Act
			var imageLink = view.Html.RouteImageLink(linkImageSource);
			var output = imageLink.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void ImageLink_WithIntWidthOnly() {
			// Arrange
			var linkImageSource = "~/images/dummy.jpg";
			var imageSourceOutput = "/images/dummy.jpg";
			var imageLinkUrl = "~/imageLink/dummy.aspx";
			var outputUrl = "/imageLink/dummy.aspx";
			var informedWidth = 200;
			var expectedOutput = string.Format(@"<a href=""{1}""><image src=""{0}"" width=""{2}px"" /></a>", imageSourceOutput, outputUrl, informedWidth);

			// Act
			var image = view.Html.ImageLink(linkImageSource, imageLinkUrl).Width(informedWidth);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void ImageLink_WithStringWidthOnly() {
			// Arrange
			var linkImageSource = "~/images/dummy.jpg";
			var imageSourceOutput = "/images/dummy.jpg";
			var imageLinkUrl = "~/imageLink/dummy.aspx";
			var outputUrl = "/imageLink/dummy.aspx";
			var informedWidth = "200%";
			var expectedOutput = string.Format(@"<a href=""{1}""><image src=""{0}"" width=""{2}"" /></a>", imageSourceOutput, outputUrl, informedWidth);

			// Act
			var image = view.Html.ImageLink(linkImageSource, imageLinkUrl).Width(informedWidth);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void ImageLink_WithUnitWidthOnly() {
			// Arrange
			var linkImageSource = "~/images/dummy.jpg";
			var imageSourceOutput = "/images/dummy.jpg";
			var imageLinkUrl = "~/imageLink/dummy.aspx";
			var outputUrl = "/imageLink/dummy.aspx";
			var informedWidth = new Unit(200);
			var expectedOutput = string.Format(@"<a href=""{1}""><image src=""{0}"" width=""{2}"" /></a>", imageSourceOutput, outputUrl, informedWidth.ToString());

			// Act
			var image = view.Html.ImageLink(linkImageSource, imageLinkUrl).Width(informedWidth);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void ImageLink_WithIntHeightOnly() {
			// Arrange
			var linkImageSource = "~/images/dummy.jpg";
			var imageSourceOutput = "/images/dummy.jpg";
			var imageLinkUrl = "~/imageLink/dummy.aspx";
			var outputUrl = "/imageLink/dummy.aspx";
			var informedHeight = 200;
			var expectedOutput = string.Format(@"<a href=""{1}""><image height=""{2}px"" src=""{0}"" /></a>", imageSourceOutput, outputUrl, informedHeight);

			// Act
			var image = view.Html.ImageLink(linkImageSource, imageLinkUrl).Height(informedHeight);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void ImageLink_WithStringHeightOnly() {
			// Arrange
			var linkImageSource = "~/images/dummy.jpg";
			var imageSourceOutput = "/images/dummy.jpg";
			var imageLinkUrl = "~/imageLink/dummy.aspx";
			var outputUrl = "/imageLink/dummy.aspx";
			var informedHeight = "200%";
			var expectedOutput = string.Format(@"<a href=""{1}""><image height=""{2}"" src=""{0}"" /></a>", imageSourceOutput, outputUrl, informedHeight);

			// Act
			var image = view.Html.ImageLink(linkImageSource, imageLinkUrl).Height(informedHeight);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void ImageLink_WithUnitHeightOnly() {
			// Arrange
			var linkImageSource = "~/images/dummy.jpg";
			var imageSourceOutput = "/images/dummy.jpg";
			var imageLinkUrl = "~/imageLink/dummy.aspx";
			var outputUrl = "/imageLink/dummy.aspx";
			var informedHeight = new Unit(200);
			var expectedOutput = string.Format(@"<a href=""{1}""><image height=""{2}"" src=""{0}"" /></a>", imageSourceOutput, outputUrl, informedHeight.ToString());

			// Act
			var image = view.Html.ImageLink(linkImageSource, imageLinkUrl).Height(informedHeight);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void ImageLink_WithIntSize() {
			// Arrange
			var linkImageSource = "~/images/dummy.jpg";
			var imageSourceOutput = "/images/dummy.jpg";
			var imageLinkUrl = "~/imageLink/dummy.aspx";
			var outputUrl = "/imageLink/dummy.aspx";
			var informedWidth = 300;
			var informedHeight = 200;
			var expectedOutput = string.Format(@"<a href=""{1}""><image height=""{3}px"" src=""{0}"" width=""{2}px"" /></a>", imageSourceOutput, outputUrl, informedWidth, informedHeight);

			// Act
			var image = view.Html.ImageLink(linkImageSource, imageLinkUrl).Size(informedWidth, informedHeight);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void ImageLink_WithStringSize() {
			// Arrange
			var linkImageSource = "~/images/dummy.jpg";
			var imageSourceOutput = "/images/dummy.jpg";
			var imageLinkUrl = "~/imageLink/dummy.aspx";
			var outputUrl = "/imageLink/dummy.aspx";
			var informedWidth = "100%";
			var informedHeight = "50%";
			var expectedOutput = string.Format(@"<a href=""{1}""><image height=""{3}"" src=""{0}"" width=""{2}"" /></a>", imageSourceOutput, outputUrl, informedWidth, informedHeight);

			// Act
			var image = view.Html.ImageLink(linkImageSource, imageLinkUrl).Size(informedWidth, informedHeight);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}

		/// <summary>
		///A test for Image Constructor
		///</summary>
		[TestMethod()]
		public void ImageLink_WithUnitSize() {
			// Arrange
			var linkImageSource = "~/images/dummy.jpg";
			var imageSourceOutput = "/images/dummy.jpg";
			var imageLinkUrl = "~/imageLink/dummy.aspx";
			var outputUrl = "/imageLink/dummy.aspx";
			var informedWidth = new Unit(20, UnitType.Em);
			var informedHeight = new Unit(10, UnitType.Em); ;
			var expectedOutput = string.Format(@"<a href=""{1}""><image height=""{3}"" src=""{0}"" width=""{2}"" /></a>", imageSourceOutput, outputUrl, informedWidth.ToString(), informedHeight.ToString());

			// Act
			var image = view.Html.ImageLink(linkImageSource, imageLinkUrl).Size(informedWidth, informedHeight);
			var output = image.ToString();

			// Assert
			Assert.AreEqual<string>(expectedOutput, output);
		}
	}
}
